﻿using System;
using System.Collections.ObjectModel;
using System.Data;
using System.Data.SqlClient;
using Common.Constants;
using Common.Helper;
using Entities;

namespace DataAccess
{
    public class SupportingMaterialDataAccess
    {
        /// <summary>
        /// Gets the instance.
        /// </summary>
        /// <value>
        /// The instance.
        /// </value>
        public static SupportingMaterialDataAccess Instance { get; private set; }

        /// <summary>
        /// Initializes the <see cref="AddressDataAccess" /> class.
        /// </summary>
        static SupportingMaterialDataAccess() { Instance = new SupportingMaterialDataAccess(); }



        /// <summary>
        /// Extracts the raw data.
        /// </summary>
        /// <param name="dataReader">The data reader.</param>
        /// <returns></returns>
        private static SupportingMaterial ExtractRawData(IDataRecord dataReader)
        {
            var sptMaterial = new SupportingMaterial
                                  {
                                      SupportingMaterialId = (Guid) dataReader["SupportingMaterialId"],
                                      UserId = (Guid) dataReader["UserID"],
                                      Type = dataReader["Type"].ToString(),
                                      Description = dataReader["Description"].ToString(),
                                      Url = dataReader["URL"].ToString(),
                                      IsActive = (bool) dataReader["IsActive"],
                                      OrganisationId = (Guid)dataReader["OrganisationId"],
                                      AddedDate = (DateTime)dataReader["AddedDate"]
                                  };



            return sptMaterial;


        }
        public SupportingMaterial GetSupportingMaterial(string supportingMaterialId)
        {


            var colParameter = new Collection<SqlParameter>
                                   {
                                       new SqlParameter("SupportingMaterialId", supportingMaterialId)
                                      
                                       
                                   };
            using (var reader = SqlHelper.ExecuteReader(ProcedureStores.GetSupportingMaterial, colParameter))
            {
                if (reader.Read())
                {
                    return ExtractRawData(reader);

                }
                return null;
            }
        }

       
        public Collection<SupportingMaterial> ListSupportingMaterial(string OrganisationId,bool isActive)
        {
            var lstMaterial = new Collection<SupportingMaterial>();

            var colParameter = new Collection<SqlParameter>
                                   {
                                       new SqlParameter("IsActive",isActive),
                                       new SqlParameter("OrganisationId",OrganisationId)
                                       
                                   };
            using (var reader = SqlHelper.ExecuteReader(ProcedureStores.ListSupportingMaterial, colParameter))
            {
                while (reader.Read())
                {
                    var result = ExtractRawData(reader);
                    lstMaterial.Add(result);
                }
                return lstMaterial;
            }
        }
        public int UpdateSupportingMaterial(SupportingMaterial supportingMaterial)
        {
          

            var colParameter = new Collection<SqlParameter>
                                   {
                                       new SqlParameter("SupportingMaterialID",supportingMaterial.SupportingMaterialId),
                                       new SqlParameter("URL",supportingMaterial.Url),
                                       new SqlParameter("Type",supportingMaterial.Type),
                                       new SqlParameter("Description",supportingMaterial.Description),
                                       new SqlParameter("IsActive",supportingMaterial.IsActive)
                                   };
            var rerult = SqlHelper.ExecuteNonQuery(ProcedureStores.UpdateSupportingMaterial, colParameter);
            return rerult;
        }

    }
}
